package com.example.dev_wang.appfacadedemo.Module.Login.Mediator;

import android.content.Context;
import android.widget.Toast;

import com.example.dev_wang.appfacadedemo.AppFacade.AC;
import com.example.dev_wang.appfacadedemo.AppFacade.MC;
import com.example.dev_wang.appfacadedemo.Module.Login.Interface.IResultCallback;
import com.example.dev_wang.appfacadedemo.Module.Login.ViewModel.LoginViewModel;
import com.example.puremvc_appfacade.Runs.Mediator;
import com.example.puremvc_appfacade.Runs.protocol.INotification;

import java.util.HashMap;

/**
 * Created by dev_wang on 2017/2/3.
 */

public class LoginMediator extends Mediator {

    @Override
    public void handleNotification(INotification notification) {
        super.handleNotification(notification);

        String name = notification.getName();

        if (name.equals(MC.BIND_LOGIN_VIEW_COMPONENT)) {
            Object object = notification.getObject();
            if (null != object) {
                this.setViewComponent(object);
                Toast.makeText((Context)object, MC.USER_LOGIN_NOTIFICATION, Toast.LENGTH_SHORT).show();
            }
            return;
        }

        if (name.equals(MC.USER_LOGIN_NOTIFICATION)) {
            HashMap<String, String> loginInfoMap = (HashMap<String, String>)notification.getObject();
            this.userLogin(loginInfoMap);
            return;
        }

        if (name.equals(MC.USER_LOGOUT_NOTIFICATION)) {
            this.loginViewModel().requestUserLogout(new IResultCallback() {
                @Override
                public void onSuccess() {
                    Toast.makeText((Context)getViewComponent(),"登出成功",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailed() {
                    Toast.makeText((Context)getViewComponent(),"登出失败",Toast.LENGTH_SHORT).show();
                }
            });

            return;
        }

        if (name.equals(MC.USER_REGISTER_NOTIFICATION)) {
            HashMap<String, String> registerInfoMap = (HashMap<String, String>)notification.getObject();
            this.userRegister(registerInfoMap);
            return;
        }
    }


    private void userLogin(HashMap<String, String> loginInfoMap) {
        String userName = loginInfoMap.get(AC.USER_NAME_KEY);
        String userPassword= loginInfoMap.get(AC.USER_PASSWORD_KEY);
        if (!isVaild(userName, userPassword)) {
            Toast.makeText((Context)getViewComponent(),"非法用户名或者密码",Toast.LENGTH_SHORT).show();
            return;
        }

        this.loginViewModel().requestUserLogin(userName, userPassword, new IResultCallback() {
            @Override
            public void onSuccess() {
                Toast.makeText((Context)getViewComponent(),"登录成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed() {
                Toast.makeText((Context)getViewComponent(),"登录失败",Toast.LENGTH_SHORT).show();
            }
        });

        LoginViewModel.self().requestUserLogin(userName, userPassword, new IResultCallback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onFailed() {

            }
        });
    }

    private void userRegister(HashMap<String, String> registerInfoMap) {
        String userName = registerInfoMap.get(AC.USER_NAME_KEY);
        String userPassword= registerInfoMap.get(AC.USER_PASSWORD_KEY);
        if (!isVaild(userName, userPassword)) {
            Toast.makeText((Context)getViewComponent(),"非法用户名或者密码",Toast.LENGTH_SHORT).show();
            return;
        }

        this.loginViewModel().requestUserRegister(userName, userPassword, new IResultCallback() {
            @Override
            public void onSuccess() {
                Toast.makeText((Context)getViewComponent(),"注册成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed() {
                Toast.makeText((Context)getViewComponent(),"注册失败",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private boolean isVaild(String name, String passwd) {
        return name.length() >= 6 && passwd.length() >= 6;
    }

    private LoginViewModel loginViewModel() {
        return (LoginViewModel)MC.FACADE.getViewModelWithName(LoginViewModel.class.getName());
    }

    @Override
    public String[] listNotificationInterests() {
        return new String[]{MC.BIND_LOGIN_VIEW_COMPONENT,
                MC.USER_LOGIN_NOTIFICATION,
                MC.USER_LOGOUT_NOTIFICATION,
                MC.USER_REGISTER_NOTIFICATION};
    }
}
